package org.hackystat.devcathlon.ui.page.commons.panels.match;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.image.resource.BufferedDynamicImageResource;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.hackystat.devcathlon.engine.match.Match;
import org.hackystat.devcathlon.engine.match.MatchManager;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.ui.DevcathlonSession;
import org.hackystat.devcathlon.ui.page.commons.panels.event.EventsPanel;
import org.hackystat.devcathlon.ui.page.teams.profile.TeamsProfile;
import org.hackystat.utilities.tstamp.Tstamp;

/**
 * Match card panel that contains the scores of two opposing teams and their match events.
 * @author Anthony Du
 *
 */
public class MatchCardPanel extends Panel {
  /** Support Serialization. */
  private static final long serialVersionUID = 1L;
  
  /**
   * Match card panel constructor.
   * @param id the panel id.
   * @param match the match.
   */
  public MatchCardPanel(String id, final Match match) {
    super(id);
    // Match name
    final String matchName = match.getName();
    add(new Label("matchName", matchName));
    // List of teams in match
    IModel listModel = new AbstractReadOnlyModel() {
      private static final long serialVersionUID = 1L;

      public Object getObject() {
        return new ArrayList<Team>(match.getTeams());
      }
    };
    add(new ListView("teams", listModel) {
      /** Support Serialization. */
      private static final long serialVersionUID = 1L;
      
      @Override
      protected void populateItem(final ListItem item) {
        final Team team = (Team) item.getModelObject();
        // Linkable team logo
        Link teamLogoLink = new Link("teamLogoLink") {
          private static final long serialVersionUID = 1L;
          @Override
          public void onClick() { 
            DevcathlonSession.get().getTeamsSession().setSelectedTeam(team.getName());
            setResponsePage(new TeamsProfile());
          }          
        };
        // Team logo
        try {
          BufferedDynamicImageResource resource = new BufferedDynamicImageResource();     
          BufferedImage image;      
          image = ImageIO.read(new File(team.getTeamLogoPath()));
          resource.setImage(image);
          teamLogoLink.add(new Image("teamLogo", resource)); 
        }
        catch (IOException e) {
          teamLogoLink.add(new Image("teamLogo"));
        }
        item.add(teamLogoLink);
        // Team name link
        Link teamNameLink = new Link("teamNameLink") {
          private static final long serialVersionUID = 1L;
          @Override
          public void onClick() { 
            DevcathlonSession.get().getTeamsSession().setSelectedTeam(team.getName());
            setResponsePage(new TeamsProfile());
          }          
        };
        teamNameLink.add(new Label("teamName", team.getName()));
        item.add(teamNameLink);
        // Team score
        Integer teamScore = MatchManager.getInstance().getMatch(matchName).getTeamPoints(team);
        item.add(new Label("teamScore", teamScore.toString()));
      }
    });
    // List all related events
    // Display count-down if match is still in progress, else "final" (match-status)
    String status = "";
    try {
      if (Tstamp.lessThan(Tstamp.makeTimestamp(), MatchManager.getInstance()
          .getMatch(matchName).getEndTime())) {
        status = "Ends " + MatchManager.getInstance().getMatch(matchName).printEndDateTime();
      }
      else {
        status = "Final";
      }
    }
    catch (Exception e) {
      // if there is an Tstamp error
      e.printStackTrace();
    }
    add(new Label("matchStatus", status));
    add(new EventsPanel("eventsPanel", match));
  }
}
